package us.icitap.cm.ui.shared;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.entities.UniqueNumber;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustHorizontalLayout;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.utils.HandleNotification;
import us.icitap.utils.Result;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.converter.Converter.ConversionException;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.UserError;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Runo;

public class UniqueNumberField extends CustomField<UniqueNumber>{

	private CustTextField unTextField = new CustTextField();


	
	private CustButton searchButton = new CustButton("Kerko");
	private CustButton clearButton = new CustButton("Pastro");

	final Embedded infoImg24 = new Embedded(null,GeneralVars.INFO_IMG_16_RES);
	
	@Override
	protected Component initContent() {
		CustHorizontalLayout hl = new CustHorizontalLayout();
		this.setCaption("Numri unik: ");
		unTextField.setWidth("25em");
		unTextField.addStyleName("cust");
		unTextField.setImmediate(true);
		hl.addComponent(unTextField);
		hl.addComponent(infoImg24);
		hl.setComponentAlignment(infoImg24, Alignment.MIDDLE_CENTER);
		hl.addComponent(searchButton);
		hl.addComponent(clearButton);
		hl.setComponentAlignment(clearButton, Alignment.MIDDLE_CENTER);
		clearButton.addStyleName(Runo.BUTTON_LINK);

		this.searchButton.setImmediate(true);
		
		this.searchButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					if(unTextField.getValue() == null || "".equals(unTextField.getValue().trim())){
						HandleNotification.showInfoOnTray("Shkruani numrin unik per te cilin doni te kerkoni");
						return;
					}
					Result<UniqueNumber> result = CaseManagementUI.appStatelessBean.getPersistenceBean().findUniqueNumber(unTextField.getValue());
					if(result.isSucceeded()){
						if(result.getObject() != null)
							UniqueNumberField.this.loadUniqueNumber(result.getObject());
						else
							HandleNotification.showInfoOnTray("Ky numer unik nuk ekziston");
					}
					else{
						HandleNotification.showInfoOnTray(result.getMessage());
						HandleNotification.showInfoOnTray("ASGJE");
					}
				} catch (Exception e) {
					e.printStackTrace();
//					searchButton.setComponentError(new UserError("Gabim"));
				}
			}
		});
		
		clearButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				clearUniqueNumber();
			}
		});
		
		this.unTextField.setImmediate(true);
		this.unTextField.addTextChangeListener(new TextChangeListener() {
			
			@Override
			public void textChange(TextChangeEvent event) {
				setValue(null);
				infoImg24.setDescription(null);
				searchButton.setDescription(null);
				searchButton.setIcon(null);
//				searchButton.setComponentError(null);
				
			}
		});
		
		
		infoImg24.setDescription("Nuk eshte zgjedhur asnje numer unik");
		
		return hl;
	}

	
	private void loadUniqueNumber(UniqueNumber un){
		infoImg24.setDescription("Oficeri qe ka kerkuar numrin unik: " + un.getRequestingOfficer().getNameSurname() + ", " +un.getOrgStructure().getName());
		if(un.getEventReport() != null){
			this.searchButton.setIcon(GeneralVars.EXCLAMATION_16_RES);
			searchButton.setDescription("Per kete numer unik eshte plotesuar nje raport ngjarje");
			this.setValue(null);
		}
		else{
			this.searchButton.setIcon(GeneralVars.CHECK_MARK_16_RES);
			this.setValue(un);
		}
				
	}
	
	private void clearUniqueNumber(){
		this.setValue(null);
		this.unTextField.setValue(null);
		this.searchButton.setIcon(null);
		this.searchButton.setDescription(null);
		this.infoImg24.setDescription(null);
	}
	
	@Override
	public Class<? extends UniqueNumber> getType() {
		return UniqueNumber.class;
	}


	



	@Override
	protected void setInternalValue(UniqueNumber newValue) {
		super.setInternalValue(newValue);
		if(this.getValue() != null)
			this.unTextField.setValue(this.getValue().getUniqueNumber());
		else
			this.unTextField.setValue(null);
	}

	
	
}
